package us.mooresr.geartrack.client.core.desktop;

import java.util.List;

import us.mooresr.geartrack.client.core.presenter.ItemPresenter;
import us.mooresr.geartrack.client.core.presenter.ItemUiHandlers;
import us.mooresr.geartrack.shared.model.DatastoreObject;
import us.mooresr.geartrack.shared.model.Item;
import us.mooresr.geartrack.shared.model.ItemType;
import us.mooresr.geartrack.shared.model.Location;

import com.gwtplatform.mvp.client.ViewWithUiHandlers;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class DesktopItemViewImpl 
	extends ViewWithUiHandlers<ItemUiHandlers> 
	implements ItemPresenter.MyView, ContentWidgetEventHandler
	{

	private final Widget widget;
	private final ContentWidget contentWidget;
	private final ItemForm itemForm;
	@UiField HTMLPanel contentPanel;
	private Item selectedItem;

	public interface Binder extends UiBinder<Widget, DesktopItemViewImpl> {
	}

	@Inject
	public DesktopItemViewImpl(final Binder binder, final EventBus eventBus) {
		widget = binder.createAndBindUi(this);
		contentWidget = GWT.create(ContentWidget.class);
		contentWidget.setTitle("Items");
		contentPanel.add(contentWidget);
		contentWidget.setHandler(this);
		
		itemForm = new ItemForm();
		contentWidget.setContent(itemForm);
		
		// Assume we start with no item selected and an empty form, so disable the save and delete buttons.
		contentWidget.disableButton(1);
		contentWidget.disableButton(2);
	
	}

		

	@Override
	public Widget asWidget() {
		return widget;
	}

	@Override
	public void setItemList(List<Item> items) {
		contentWidget.setListContents(items);
		clearSelection();
		
	}


	@Override
	public void onButton1Click() {
		// Save Button
		if (!validate()) {
			return;
		}

		// End of validation
		selectedItem.setName(itemForm.getName());
		selectedItem.setDescription(itemForm.getDescription());
		
		int serial = 0;
		String serialString = itemForm.getSerial();
		if ((serialString != null) && (serialString.length() > 0)) {
			try {
				serial = Integer.parseInt(serialString);
			} catch (NumberFormatException e) {
				// Invalid serial number
				// This shouldn't happen - we checked for this in validate()
			}
		}
		selectedItem.setSerial(serial);
		
		selectedItem.setHomeLocationId(itemForm.getLocationId());
		
		selectedItem.setItemTypeId(itemForm.getItemTypeId());

		getUiHandlers().updateItem(selectedItem);
		
		itemForm.clear();
		
	}


	@Override
	public void onButton2Click() {
		// Delete Button
		if (selectedItem == null) {
			return;
		}
		
		String name = selectedItem.getName();
		String message = "Are you sure you want to delete " + name + "?";
		boolean answer = Window.confirm(message);
		
		if (answer) {
			getUiHandlers().deleteItem(selectedItem);
			itemForm.clear();
		}
		
	}


	@Override
	public void onButton3Click() {
		// Add Button


		if (!validate()) {
			return;
		}

		// End of validation
		Item newItem = new Item();
		newItem.setName(itemForm.getName());
		newItem.setDescription(itemForm.getDescription());
		
		int serial = 0;
		String serialString = itemForm.getSerial();
		if ((serialString != null) && (serialString.length() > 0)) {
			try {
				serial = Integer.parseInt(serialString);
			} catch (NumberFormatException e) {
				// Invalid serial number
				// This shouldn't happen - we checked for this in validate()
			}
		}
		newItem.setSerial(serial);
		
		newItem.setHomeLocationId(itemForm.getLocationId());
		
		newItem.setItemTypeId(itemForm.getItemTypeId());

		getUiHandlers().addItem(newItem);
		
		itemForm.clear();
	}

	private boolean validate() {
		@SuppressWarnings("unused")
		int serial=0;
		
		String name = itemForm.getName();
		if ((name == null) || (name.length() == 0)) {
			// Must have a name
			Window.alert("Name can not be blank when adding a new item.");
			return false;
		}

		String serialString = itemForm.getSerial();
		if ((serialString != null) && (serialString.length() > 0)) {
			try {
				serial = Integer.parseInt(serialString);
			} catch (NumberFormatException e) {
				// Invalid serial number
				Window.alert("Serial number must be blank or a number.");
				return false;
			}
		}
		
		return true;

	}


	@Override
	public void addItemToList(Item item) {
		contentWidget.addItemToList(item);
		
	}


	/**
	 * Called when the user has selected in item in the list inside the ContentWidget.
	 */
	@Override
	public void onSelection(DatastoreObject selectedObject) {
		selectedItem = (Item)selectedObject;
		
		itemForm.setName(selectedItem.getName());
		itemForm.setDescription(selectedItem.getDescription());
		itemForm.setSerial(selectedItem.getSerial());
		itemForm.setLocationId(selectedItem.getHomeLocationId());
		itemForm.setItemTypeId(selectedItem.getItemTypeId());
		
		contentWidget.enableButton(1);
		contentWidget.enableButton(2);
	}



	@Override
	public void removeItemFromList(Item item) {
		contentWidget.removeItemFromList(item);
		clearSelection();
		
	}



	@Override
	public void updateListEntry(Item item) {
		contentWidget.updateListItem(item);
		clearSelection();
		
	}
	
	private void clearSelection() {
		selectedItem = null;
		contentWidget.clearSelection();
		contentWidget.disableButton(1);
		contentWidget.disableButton(2);
	}



	@Override
	public void setLocations(List<Location> locations) {
		itemForm.setLocations(locations);
		
	}



	@Override
	public void setItemTypes(List<ItemType> itemTypes) {
		itemForm.setItemTypes(itemTypes);
		
	}



	@Override
	public void addLocation(Location location) {
		itemForm.addLocation(location);
		
	}



	@Override
	public void removeLocation(Location location) {
		itemForm.removeLocation(location);
		
	}



	@Override
	public void changeLocation(Location location) {
		itemForm.changeLocation(location);
		
	}



	@Override
	public void addItemType(ItemType itemType) {
		itemForm.addItemType(itemType);
		
	}



	@Override
	public void removeItemType(ItemType itemType) {
		itemForm.removeItemType(itemType);
		
	}



	@Override
	public void changeItemType(ItemType itemType) {
		itemForm.changeItemType(itemType);
		
	}
}
